package com.atguigu.daijia.order.service.impl;

import com.atguigu.daijia.common.constant.RedisConstant;
import com.atguigu.daijia.common.execption.GuiguException;
import com.atguigu.daijia.common.result.ResultCodeEnum;
import com.atguigu.daijia.model.entity.order.OrderInfo;
import com.atguigu.daijia.model.entity.order.OrderStatusLog;
import com.atguigu.daijia.model.enums.OrderStatus;
import com.atguigu.daijia.model.form.order.OrderInfoForm;
import com.atguigu.daijia.order.mapper.OrderInfoMapper;
import com.atguigu.daijia.order.mapper.OrderStatusLogMapper;
import com.atguigu.daijia.order.service.OrderInfoService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.redisson.api.RedissonClient;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.UUID;
import java.util.concurrent.TimeUnit;

@Service
@SuppressWarnings({"unchecked", "rawtypes"})
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {
@Autowired
OrderInfoMapper orderInfoMapper;
@Autowired
    OrderStatusLogMapper orderStatusLogMapper;
@Autowired
    RedisTemplate redisTemplate;
@Transactional(rollbackFor = {Exception.class})
    @Override
    public Long saveOrderInfo(OrderInfoForm orderInfoForm) {
    OrderInfo orderInfo = new OrderInfo();
    BeanUtils.copyProperties(orderInfoForm,orderInfo);
    String orderNo= UUID.randomUUID().toString().replaceAll("-", "");
    orderInfo.setStatus(OrderStatus.WAITING_ACCEPT.getStatus());
    orderInfo.setOrderNo(orderNo);
    orderInfoMapper.insert(orderInfo);
    //记录日志
    this.log(orderInfo.getId(),orderInfo.getStatus());
    //接单标识，标识不存在了说明不在等待接单状态了
    redisTemplate.opsForValue().set(RedisConstant.ORDER_ACCEPT_MARK,"0",RedisConstant.ORDER_ACCEPT_MARK_EXPIRES_TIME, TimeUnit.MINUTES);
    return orderInfo.getId();
}

    @Override
    public Integer getOrderStatus(Long orderId) {
        LambdaQueryWrapper<OrderInfo> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(OrderInfo::getId,orderId);
        queryWrapper.select(OrderInfo::getStatus);
        OrderInfo orderInfo = orderInfoMapper.selectOne(queryWrapper);
        if (null==orderInfo){
            //返回null,feignClient解析会抛异常 给默认值 后续会用
            return OrderStatus.NULL_ORDER.getStatus();
        }
        return orderInfo.getStatus();
    }
//@Autowired
//private RedissonClient redissonClient;
@Transactional(rollbackFor = {Exception.class})
    @Override
    public Boolean robNewOrder(Long driverId, Long orderId) {
        //抢单成功或取消订单 都会删除该Key redis判断 减少数据库压力
    if (!redisTemplate.hasKey(RedisConstant.ORDER_ACCEPT_MARK)){
        //枪弹失败
        throw new GuiguException(ResultCodeEnum.COB_NEW_ORDER_FAIL);
    }

    //修改订单状态及司机id
    //update order_info set status = 2, driver_id = #{driverId}, accept_time = now() where id = #{id}
    //修改字段
    OrderInfo orderInfo = new OrderInfo();
    orderInfo.setId(orderId);
    orderInfo.setStatus(OrderStatus.ACCEPTED.getStatus());
    orderInfo.setAcceptTime(new Date());
    orderInfo.setDriverId(driverId);
    int rows=orderInfoMapper.updateById(orderInfo);
    if (rows !=1) {
        //枪弹失败
        throw new GuiguException(ResultCodeEnum.COB_NEW_ORDER_FAIL);
    }

    //记录日志
    this.log(orderId,orderInfo.getStatus());
    //删除redis订单标识
    redisTemplate.delete(RedisConstant.ORDER_ACCEPT_MARK);
    return true;
    }


    private void log(Long orderId, Integer status) {
        OrderStatusLog orderStatusLog = new OrderStatusLog();
        orderStatusLog.setOrderId(orderId);
        orderStatusLog.setOrderStatus(status);
        orderStatusLog.setOperateTime(new Date());
        orderStatusLogMapper.insert(orderStatusLog);

    }


}
